﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceGame
{
    public abstract class GameObject :  IRenderable
    {
        protected Position topLeft;

        protected char[,] image;

        public bool IsDestroyed { get; protected set; }

        public Position TopLeft
        {
            get
            {
                return new Position(this.topLeft.Row, this.topLeft.Col);
            }
            set
            {
                this.topLeft = new Position(value.Row, value.Col);
            }
        }

        public char[,] Image
        {
            get 
            {
                return CloneImage(this.image);
            }
            set
            {
                this.image = CloneImage(value);
            }
        }

        protected GameObject(Position topLeft, char[,] image)
        {
            this.TopLeft = topLeft;

            this.Image = image;

            this.IsDestroyed = false;
        }
        
        public abstract void Update();

        private char[,] CloneImage(char[,] image)
        {
            int rows = image.GetLength(0);
            int cols = image.GetLength(1);

            char[,] newImage = new char[rows, cols];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    newImage[row, col] = image[row, col];
                }
            }

            return newImage;
        }

        public virtual IEnumerable<GameObject> CreateObjects()
        {
            return new List<GameObject>();
        }
    }
}
